<template>
  <div>
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="120px">
      <el-row :gutter="20">
        {{range $i, $v := .Fields}}<el-col :span="12">
          <el-form-item size="mini" label="{{$v.Comment}}" prop="{{$v.GoName}}">
            <el-input v-model="dataForm.{{$v.GoName}}" maxlength="25" show-word-limit placeholder="请输入{{$v.Comment}}" />
          </el-form-item>
        </el-col>{{end}}
      </el-row>
    </el-form>
    <el-row>
      <el-col style="text-align: right;">
        <el-button size="mini" icon="el-icon-close" @click="visible = false">取消</el-button>
        <el-button size="mini" icon="el-icon-check" type="primary" @click="dataFormSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
  import { get{{.TypeName}}, save{{.TypeName}}, update{{.TypeName}}} from '@/api/{{.FileName}}'
  export default {
    props: ['Id'],
    data() {
      return {
        dataForm: {
          {{range $i, $v := .Fields}} {{$v.GoName}}: '',
          {{end}}
        },
        dataRule:{
          {{range $i, $v := .Fields}} {{$v.GoName}}: '',
          {{end}}
        }
      }
    },
    mounted(){
      if(this.Id && this.Id > 0) {
        this.init()
      }
    },
    methods: {
      init() {
        let _this = this
        _this.$refs.dataForm.resetFields()
        if(_this.Id && this.Id > 0) {
          get{{.TypeName}}(_this.Id).then( (data) => {
            if (data && data.code === '20000'){
              _this.dataForm = data.{{.TypeName}}
            }
          })
        }else{

        }
      },
      cancel(){
        this.$emit("closeable")
      },
      dataFormSubmit(){
        var permission
        if (this.Id && this.Id > 0) {
          permission = update{{.TypeName}}(this.dataForm)
        } else {
          permission = save{{.TypeName}}(this.dataForm)
        }
        permission.then((data) => {
          if (data && data.code === '20000') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.$emit('closeable')
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>
